package com.leancloud.circle.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSONArray;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.leancloud.circle.LoginCallUtil;
import com.leancloud.circle.R;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.bean.CollectionBean;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;

import java.util.List;

import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.user.CurrentUser;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.OnNoDoubleClickListener;
import tech.com.commoncore.utils.ToastUtil;

import static com.leancloud.circle.manage.CirclePathManage.CIRCLE_TOPIC_DETAILS;

public class CircleAdapter extends BaseQuickAdapter<CircleBean, BaseViewHolder> {

    public CircleAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, final CircleBean item) {

//        helper.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ARouter.getInstance().build(CIRCLE_ARTICLE_DETAILS)
//                        .withInt("communityId", item.getId())
//                        .greenChannel()
//                        .navigation();
//            }
//        });

        helper.itemView.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ARouter.getInstance().build(CIRCLE_TOPIC_DETAILS)
                        .withInt("communityId", item.getId())
                        .greenChannel()
                        .navigation();
            }
        });

        final TextView collectionTx = helper.itemView.findViewById(R.id.circle_item_collection_tx);
        final TextView commentTx = helper.itemView.findViewById(R.id.circle_item_comment_tx);
        final TextView likeTx = helper.itemView.findViewById(R.id.circle_item_like_tx);
        final TextView attentionTx = helper.itemView.findViewById(R.id.circle_item_attention_tx);
        final TextView browseTx = helper.itemView.findViewById(R.id.circle_item_browse_tx);
        final ImageView headImg = helper.itemView.findViewById(R.id.circle_item_head_image);
        final RecyclerView imagesRecycler = helper.itemView.findViewById(R.id.circle_item_img_recycler);

        String nikeName = item.getUser().getUser_nickname();
        String headIcon = item.getUser().getAvatar();
        String date = item.getCreate_time();
        String content = item.getPost_content();

        List<String> images = JSONArray.parseArray(item.getMore(), String.class);
        String commentCount = String.valueOf(item.getComment_count());
        String likeCount = String.valueOf(item.getPost_like());
        String browseCount = String.valueOf(item.getPost_hits());

        GlideManager.loadCircleImg(headIcon, headImg, R.mipmap.circle_head);

        CirclePhotoAdapter photoAdapter = new CirclePhotoAdapter(R.layout.circle_layout_item_circle_photo);
        imagesRecycler.setAdapter(photoAdapter);
        imagesRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        if (images != null && !images.isEmpty()) {
            photoAdapter.replaceData(images);
        }
        photoAdapter.notifyDataSetChanged();


        collectionTx.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
                    @Override
                    protected void passBack() {
                        handlerCollectionClick(collectionTx, item);
                    }
                });
            }
        });

//        commentTx.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ARouter.getInstance().build(CIRCLE_ARTICLE_DETAILS)
//                        .withInt("communityId", item.getId())
//                        .greenChannel()
//                        .navigation();
//            }
//        });
        likeTx.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
                    @Override
                    protected void passBack() {
                        handlerLikeClick(likeTx, item);
                    }
                });
            }
        });
        attentionTx.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                LoginCallUtil.verify_Toset(new LoginCallUtil.Back() {
                    @Override
                    protected void passBack() {
                        handlerAttentionClick(attentionTx, item);
                    }
                });
            }
        });
        helper.itemView.findViewById(R.id.circle_item_share_tx).setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                FastUtil.startShareText(mContext, item.getPost_content());
            }
        });

        if (nikeName == null || nikeName.isEmpty()){
            helper.setText(R.id.circle_item_name_tx, item.getUser().getMobile());
        }else {
            helper.setText(R.id.circle_item_name_tx, nikeName);
        }

        helper.setText(R.id.circle_item_date_tx, date);
        helper.setText(R.id.circle_item_content_tx, content);
//        helper.setText(R.id.circle_item_collection_tx, collectionCount);
        helper.setText(R.id.circle_item_comment_tx, commentCount);
        helper.setText(R.id.circle_item_like_tx, likeCount);
        helper.setText(R.id.circle_item_browse_tx, browseCount);

        attentionTx.setSelected(CircleGlobal.isConcerned(item.getUser().getId()));
        collectionTx.setSelected(CircleGlobal.isCollections(item.getId()));
    }

    private void handlerCollectionClick(TextView tx, CircleBean circleBean) {
        if (!tx.isSelected()) {
            CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
            CircleGlobal.getCircleImp().addCollection(user.getToken(), circleBean.getId(), new CircleBack() {
                @Override
                public void onBack(Object o, String err) {
                    if (err == null) {
                        ToastUtil.show("收藏成功");
                        CircleGlobal.updateCollection();
                    }
                }
            });
        } else {
            CollectionBean bean = CircleGlobal.getCollection(circleBean.getId());
            CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
            CircleGlobal.getCircleImp().removeCollection(user.getToken(), bean.getId(), new CircleBack() {
                @Override
                public void onBack(Object o, String err) {
                    if (err == null) {
                        CircleGlobal.updateCollection();
                    }
                }
            });
        }
        tx.setSelected(!tx.isSelected());
    }

    //    https://d.wanjinig.cn/new_api/Circle/setZans
    private void handlerLikeClick(final TextView tx, CircleBean circleBean) {
        if (!tx.isSelected()) {
            CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
            CircleGlobal.getCircleImp().likeCircle(user.getToken(), circleBean.getId() + "", new CircleBack() {
                @Override
                public void onBack(Object o, String err) {
                    if (err == null) {
                        ToastUtil.show("点赞成功");
                        int likeCount = Integer.parseInt(tx.getText().toString());
                        tx.setText(likeCount + 1 + "");
                        tx.setSelected(true);
                    }
                }
            });
        }
        else {
            int likeCount = Integer.parseInt(tx.getText().toString());
            tx.setText(likeCount - 1 + "");
            tx.setSelected(false);
        }

//        if (!tx.isSelected()) {
//            int likeCount = Integer.parseInt(tx.getText().toString());
//            tx.setText(likeCount + 1 + "");
//            tx.setSelected(true);
//        }
// else {
//            int likeCount = Integer.parseInt(tx.getText().toString());
//            tx.setText(likeCount - 1 + "");
//            tx.setSelected(false);
//        }
    }

    private void handlerAttentionClick(TextView tx, CircleBean circleBean) {
        String obUserId = String.valueOf(circleBean.getUser().getId());
        CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
        CircleGlobal.getCircleImp().attentionAddOrRemove(user.getToken(), obUserId, new CircleBack() {
            @Override
            public void onBack(Object o, String err) {
                if (err == null) {
                    CircleGlobal.updateConcerned();
                }
            }
        });
        tx.setSelected(!tx.isSelected());
    }
}
